package www.qiyue.chengtou2;




import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class DanJia {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("ct2qi")
    public String test(){
        String sql = "SELECT * from reis_contract";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : list) {
            calculationSubmitted(map);
        }
        return "haole";
    }

    private Map<String, Object> calculationSubmitted(Map<String, Object> params) {
        Long id = Long.parseLong(params.get("id").toString());

        BigDecimal conpricess = ConvertUtils.toBigDecimal(params.get("conprice"), null);
        // 获取年租金 /面积/租金定义方式
        BigDecimal annualRent = ConvertUtils.toBigDecimal(params.get("annual_rent"), null);
        BigDecimal acreage = ConvertUtils.toBigDecimal(params.get("acreage"), null);
        BigDecimal mult = acreage.multiply(new BigDecimal(365));
        String rentType = params.get("rent_type").toString();

        // 计算年租金
        switch (rentType == null ? "" : rentType) {
            case "R":
                // 日租金即为租金单价 年租金
                BigDecimal dateRent = ConvertUtils.toBigDecimal(params.get("date_rent"), null);
                if (!conpricess.equals(dateRent)) {
                    String update = "UPDATE reis_contract set conprice= '" + dateRent + "' WHERE id=" + id + "";
                    jdbcTemplate.update(update);
                }
            case "N":
                // 通过年租金计算租金单价
                BigDecimal conprice = annualRent.divide(mult, 2, BigDecimal.ROUND_UP);
                if (!conpricess.equals(conprice)) {
                    String updates = "UPDATE reis_contract set conprice= '" + conprice + "' WHERE id=" + id + "";
                    jdbcTemplate.update(updates);
                }

        }
        return params;
    }
}
